import { defineStore } from "pinia";
import { getImageUrl } from "@/utils/image.js";

/**
 * 当前聊天用户信息
 */
export const CurrentChatUserInfoStore = defineStore("currentChatUserInfo", {
  state: () => ({
    currentChatUserInfo: null,
  }),
  getters: {
    /**
     * 获取处理后的当前聊天用户信息
     * @returns {Object|null} 当前聊天用户信息对象或 null
     */
    getCurrentChatUserInfo: (state) => {
      if (!state.currentChatUserInfo) return null;

      return {
        ...state.currentChatUserInfo,
        avatar: getImageUrl(state.currentChatUserInfo.avatar, "/static/avatar.png"),
      };
    },
  },
  actions: {
    /**
     * 设置当前聊天用户信息
     * @param {Object|null} data - 当前聊天用户信息对象或 null
     */
    setCurrentChatUserInfo(data) {
      this.currentChatUserInfo = data || null;
    },
    /**
     * 清除当前聊天用户信息
     */
    clearCurrentChatUserInfo() {
      this.currentChatUserInfo = null;
    },
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: "currentChatUserInfo",
        storage: localStorage,
      },
    ],
  },
});